package mc.support.taobao.api;

import mc.support.taobao.api.fields.Fields;
import mc.support.taobao.api.fields.GetItemsFields;
import com.taobao.api.TaobaoRequest;
import com.taobao.api.request.ItemsGetRequest;
import com.taobao.api.response.ItemsGetResponse;
import org.apache.commons.lang.StringUtils;

import java.util.HashSet;
import java.util.Set;

/**
 * Created with IntelliJ IDEA. <br/>
 * User: francis    <br/>
 * Date: 13-11-21    <br/>
 * Time: 10:56  <br/>
 */
public class GetItemsReq extends AbstractReq<GetItemsReq, ItemsGetResponse> {

    //limited by taobao api
    private static final int MAX_NICK_NUM = 5;

    private Fields fields;
    private String q;
    private Long cid;
    private Set<String> nicks;
    private Long pageNo;
    private Long pageSize;
    private String orderBy;
    private String props;

    private static String convertToString(Set<String> set) {
        StringBuilder s = new StringBuilder();

        int num = set.size();
        int i = 0;
        for ( String n : set ) {
            if ( i < num - 1 ) {
                s.append( n ).append( ',' );
            } else {
                s.append( n );
            }
            i++;
        }

        return s.toString();
    }

    @Override
    protected TaobaoRequest toRequest() {
        ItemsGetRequest req = new ItemsGetRequest();

        if ( null != fields ) {
            req.setFields( fields.toString() );
        }

        if ( null != nicks ) {
            req.setNicks( convertToString( nicks ) );
        }

        if ( null != pageNo ) {
            req.setPageNo( pageNo );
        }

        if ( null != pageSize ) {
            req.setPageSize( pageSize );
        }

        if ( null != q ) {
            req.setQ( q.trim() );
        }

        if ( null != cid ) {
            req.setCid( cid );
        }

        if ( null != orderBy && StringUtils.isNotEmpty(orderBy)) {
            req.setOrderBy( orderBy );
        }

        if ( null != props && StringUtils.isNotEmpty(props)) {
            req.setProps( props );
        }

        return req;
    }

    public GetItemsReq fields(GetItemsFields fields) {
        this.fields = fields;
        return this;
    }

    public GetItemsReq q(String q) {
        this.q = q;
        return this;
    }

    public GetItemsReq orderBy(String orderBy) {
        this.orderBy = orderBy;
        return this;
    }

    public GetItemsReq props(String props) {
        this.props = props;
        return this;
    }

    public GetItemsReq cid(Long cid) {
        this.cid = cid;
        return this;
    }

    public GetItemsReq addNick(String nick) {
        if ( StringUtils.isNotBlank(nick) ) {
            addToNicks( nick  );
        }
        return this;
    }

    public GetItemsReq removeNick(String nick) {
        removeFromNicks( nick );
        return this;
    }

    private void addToNicks( String nick ) {
        if ( null == nicks ) {
            nicks = new HashSet<String>();
        }
        if ( nicks.size() < MAX_NICK_NUM ) {
            nicks.add( nick );
        }
    }

    private void removeFromNicks( String nick ) {
        if ( null != nicks ) {
            nicks.remove( nick );
        }
    }

    public GetItemsReq pageNo(Long pageNo) {
        this.pageNo = pageNo;
        return this;
    }

    public GetItemsReq pageSize(Long pageSize) {
        this.pageSize = pageSize;
        return this;
    }

}
